﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using Core.DAL;

namespace BAL.eCorp
{
    public class B30BizDocs
    {
        #region region: Khai báo hằng
        const string CONST_TABLE_NAME = "B30BizDoc";
        #endregion
        
        #region region: Khai báo biến
        protected int _Id;
	    protected int _ParentId;
	    protected bool _IsGroup;
	    protected string _BizDocId = string.Empty;
	    protected string _ParentBizDocId = string.Empty;
	    protected string _DocCode = string.Empty;
	    protected DateTime _DocDate;
	    protected string _DocNo = string.Empty;
	    protected string _Description = string.Empty;
	    protected string _CustomerCode = string.Empty;
        protected string _CustomerName = string.Empty;
	    protected string _ContactCode = string.Empty;
	    protected string _ContactPerson = string.Empty;
	    protected string _Address = string.Empty;
	    protected string _CurrencyCode = string.Empty;
	    protected float _ExchangeRate = 1;
	    protected string _ShipViaCode = string.Empty;
	    protected int _DocStatus;
	    protected int _DueDate;
	    protected DateTime _HandoverDate;
	    protected DateTime _FinishedDate;
	    protected DateTime _ConfirmedDate;
	    protected float _DiscountRate;
	    protected double _DiscountAmount;
	    protected double _OriginalDiscountAmount;
	    protected int _Closed;
        // Hệ thống
	    protected bool _IsActive = true;
	    protected int _CreatedBy;
	    protected DateTime _CreatedAt;
	    protected int _ModifiedBy;
	    protected DateTime _ModifiedAt;
        #endregion

        #region region: Định nghĩa thuộc tính
        /// <summary>
        /// Id: Số tự động tăng khi insert
        /// </summary>
        public int Id
        {
            get { return _Id; }
            set { _Id = value; }
        }
        /// <summary>
        /// ParentId
        /// </summary>
        public int ParentId
        {
            get { return _ParentId; }
            set { _ParentId = value; }
        }
        /// <summary>
        /// IsGroup: 
        /// </summary>
        public bool IsGroup
        {
            get{return _IsGroup;}
            set{_IsGroup = value;}
        }
        /// <summary>
        /// BizDocId:
        /// </summary>
        public string BizDocId
        {
            get { return _BizDocId; }
            set { _BizDocId = value; }
        }
        /// <summary>
        /// ParentBizDocId
        /// </summary>
        public string ParentBizDocId
        {
            get { return _ParentBizDocId; }
            set { _ParentBizDocId = value; }
        }
        /// <summary>
        /// DocCode: 
        /// </summary>
        public string DocCode
        {
            get { return _DocCode; }
            set { _DocCode = value; }
        }
        /// <summary>
        /// Hình ảnh
        /// </summary>
        public DateTime DocDate
        {
            get { return _DocDate; }
            set { _DocDate = value; }
        }
        /// <summary>
        /// DocNo:
        /// </summary>
        public string DocNo
        {
            get{return _DocNo;}
            set{_DocNo = value;}
        }
        /// <summary>
        /// Description:
        /// </summary>
        public string Description
        {
            get { return _Description; }
            set { _Description = value; }
        }
        /// <summary>
        /// CustomerCode
        /// </summary>
        public string CustomerCode
        {
            get{return _CustomerCode;}
            set{_CustomerCode = value;}
        }
        /// <summary>
        /// CustomerName
        /// </summary>
        public string CustomerName
        {
            get { return _CustomerName; }
            set { _CustomerName = value; }
        }
        /// <summary>
        /// ContactCode
        /// </summary>
        public string ContactCode
        {
            get{return _ContactCode;}
            set{_ContactCode = value;}
        }
        /// <summary>
        /// ContactPerson
        /// </summary>
        public string ContactPerson
        {
            get{return _ContactPerson;}
            set{_ContactPerson = value;}
        }
        /// <summary>
        /// ContactPerson
        /// </summary>
        public string Address
        {
            get{return _Address;}
            set{_Address = value;}
        }
        /// <summary>
        /// ContactPerson
        /// </summary>
        public string CurrencyCode
        {
            get{return _CurrencyCode;}
            set{_CurrencyCode = value;}
        }
        /// <summary>
        /// ContactPerson
        /// </summary>
        public float ExchangeRate
        {
            get{return _ExchangeRate;}
            set{_ExchangeRate = value;}
        }
        /// <summary>
        /// ShipViaCode
        /// </summary>
        public string ShipViaCode
        {
            get{return _ShipViaCode;}
            set{_ShipViaCode = value;}
        }
        /// <summary>
        /// 
        /// </summary>
        public int DocStatus
        {
            get{return _DocStatus;}
            set{_DocStatus = value;}
        }
        /// <summary>
        /// 
        /// </summary>
        public int DueDate
        {
            get{return _DueDate;}
            set{_DueDate = value;}
        }
        public DateTime HandoverDate
        {
            get{return _HandoverDate;}
            set{_HandoverDate = value;}
        }
        public DateTime FinishedDate
        {
            get{return _FinishedDate;}
            set{_FinishedDate = value;}
        }
        public DateTime ConfirmedDate
        {
            get{return _ConfirmedDate;}
            set{_ConfirmedDate = value;}
        }
        /// <summary>
        /// 
        /// </summary>
        public float DiscountRate
        {
            get{return _DiscountRate;}
            set{_DiscountRate = value;}
        }
        /// <summary>
        /// 
        /// </summary>
        public double DiscountAmount
        {
            get{return _DiscountAmount;}
            set{_DiscountAmount = value;}
        }
        /// <summary>
        /// 
        /// </summary>
        public double OriginalDiscountAmount
        {
            get{return _OriginalDiscountAmount;}
            set{_OriginalDiscountAmount = value;}
        }
        /// <summary>
        /// 
        /// </summary>
        public int Closed
        {
            get{return _Closed;}
            set{_Closed = value;}
        }
        // Hệ thống
        /// <summary>
        /// 
        /// </summary>
        public bool IsActive
        {
            get{return _IsActive;}
            set{_IsActive = value;}
        }
        /// <summary>
        /// 
        /// </summary>
        public int CreatedBy
        {
            get{return _CreatedBy;}
            set{_CreatedBy = value;}
        }
        /// <summary>
        /// 
        /// </summary>
        public DateTime CreatedAt
        {
            get{return _CreatedAt;}
            set{_CreatedAt = value;}
        }
        /// <summary>
        /// 
        /// </summary>
        public int ModifiedBy
        {
            get{return _ModifiedBy;}
            set{_ModifiedBy = value;}
        }
        /// <summary>
        /// 
        /// </summary>
        public DateTime ModifiedAt
        {
            get{return _ModifiedAt;}
            set{_ModifiedAt = value;}
        }
        #endregion

        #region region: Định nghĩa phương thức
        /// <summary>
        /// 
        /// </summary>
        public B30BizDocs()
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="reader"></param>
        public B30BizDocs(SqlDataReader reader)
        {
            if (reader != null && !reader.IsClosed)
            {
                _Id = reader.GetInt32(reader.GetOrdinal("Id"));
                _ParentId = reader.GetInt32(reader.GetOrdinal("ParentId"));
                _IsGroup = reader.GetBoolean(reader.GetOrdinal("IsGroup"));
                _BizDocId = reader.GetString(reader.GetOrdinal("BizDocId"));
                _ParentBizDocId = reader.GetString(reader.GetOrdinal("ParentBizDocId"));
                _DocCode = reader.GetString(reader.GetOrdinal("DocCode"));
                _DocDate = reader.GetDateTime(reader.GetOrdinal("DocDate"));
                _DocNo = reader.GetString(reader.GetOrdinal("DocNo"));
                _Description = reader.GetString(reader.GetOrdinal("Description"));
                _CustomerCode = reader.GetString(reader.GetOrdinal("CustomerCode"));
                _CustomerName = reader.GetString(reader.GetOrdinal("CustomerName"));
                _ContactCode = reader.GetString(reader.GetOrdinal("ContactCode"));
                _ContactPerson = reader.GetString(reader.GetOrdinal("ContactPerson"));
                _Address = reader.GetString(reader.GetOrdinal("Address"));
                _CurrencyCode = reader.GetString(reader.GetOrdinal("CurrencyCode"));
                _ExchangeRate = reader.GetFloat(reader.GetOrdinal("ExchangeRate"));
                _ShipViaCode = reader.GetString(reader.GetOrdinal("ShipViaCode"));
                _DocStatus = reader.GetInt32(reader.GetOrdinal("DocStatus"));
                _DueDate = reader.GetInt32(reader.GetOrdinal("DueDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("HandoverDate")))
                    _HandoverDate = reader.GetDateTime(reader.GetOrdinal("HandoverDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("FinishedDate")))
                    _FinishedDate = reader.GetDateTime(reader.GetOrdinal("FinishedDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("ConfirmedDate")))
                    _ConfirmedDate = reader.GetDateTime(reader.GetOrdinal("ConfirmedDate"));
                _DiscountRate = reader.GetFloat(reader.GetOrdinal("DiscountRate"));
                _DiscountAmount = reader.GetDouble(reader.GetOrdinal("DiscountAmount"));
                _OriginalDiscountAmount = reader.GetDouble(reader.GetOrdinal("OriginalDiscountAmount"));
                _Closed = reader.GetInt32(reader.GetOrdinal("Closed"));
                // Hệ thống
                _IsActive = reader.GetBoolean(reader.GetOrdinal("IsActive"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy")))
                    _CreatedBy = reader.GetInt32(reader.GetOrdinal("CreatedBy"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedAt")))
                    _CreatedAt = reader.GetDateTime(reader.GetOrdinal("CreatedAt"));
                if (!reader.IsDBNull(reader.GetOrdinal("ModifiedBy")))
                    _ModifiedBy = reader.GetInt32(reader.GetOrdinal("ModifiedBy"));
                if (!reader.IsDBNull(reader.GetOrdinal("ModifiedAt")))
                    _ModifiedAt = reader.GetDateTime(reader.GetOrdinal("ModifiedAt"));
            }
        }
        /// <summary>
        /// Phương thức truy xuất sản phẩm theo ProductID
        /// </summary>
        /// <returns></returns>
        public static B30BizDocs GetByID(B30BizDocs o)
        {
            DBHelp db = new DBHelp();
            B30BizDocs obj = new B30BizDocs();

            db.AddParameter("@Id", o.Id);
            SqlDataReader reader = db.ExecuteReader("sp_B30BizDoc_SelectID", CommandType.StoredProcedure);
            if (reader.Read())
            {
                obj = new B30BizDocs(reader);
            }

            reader.Close();
            return obj;
        }
        /// <summary>
        /// Phương thức truy xuất toàn bộ dữ liệu bảng sản phẩm
        /// </summary>
        /// <returns></returns>
        public static IList<B30BizDocs> GetAll()
        {
            DBHelp db = new DBHelp();
            IList<B30BizDocs> list = new List<B30BizDocs>();
            SqlDataReader reader = db.ExecuteReader("sp_B30BizDoc_SelectAll", CommandType.StoredProcedure);
            while (reader.Read())
            {
                list.Add(new B30BizDocs(reader));
            }
            reader.Close();
            return list;
        }
       
        /// <summary>
        /// Chèn mới một sản phẩm
        /// </summary>
        /// <param name="o"></param>
        public static void Insert(B30BizDocs o)
        {
            DBHelp db = new DBHelp();

            //@_ParentId,
            db.AddParameter("@_ParentId", o.ParentId);
            //@_IsGroup,
            db.AddParameter("@_IsGroup", o.IsGroup);
            //@_BizDocId,
            db.AddParameter("@_BizDocId", o.BizDocId);
            //@_ParentBizDocId,
            db.AddParameter("@_ParentBizDocId", o.ParentBizDocId);
            //@_DocCode,
            db.AddParameter("@_DocCode", o.DocCode);
            //@_DocDate,
            db.AddParameter("@_DocDate", o.DocDate);
            //@_DocNo,
            db.AddParameter("@_DocNo", o.DocNo);
            //@_Description,
            db.AddParameter("@_Description", o.Description);
            //@_CustomerCode,
            db.AddParameter("@_CustomerCode", o.CustomerCode);
            //@_ContactCode,
            db.AddParameter("@_ContactCode", o.ContactCode);
            //@_ContactPerson,
            db.AddParameter("@_ContactPerson", o.ContactPerson);
            //@_Address,
            db.AddParameter("@_Address", o.Address);
            //@_CurrencyCode,
            db.AddParameter("@_CurrencyCode", o.CurrencyCode);
            //@_ExchangeRate,
            db.AddParameter("@_ExchangeRate", o.ExchangeRate);
            //@_ShipViaCode,
            db.AddParameter("@_ShipViaCode", o.ShipViaCode);
            //@_DocStatus,
            db.AddParameter("@_DocStatus", o.DocStatus);
            //@_DueDate,
            db.AddParameter("@_DueDate", o.DueDate);
            //@_HandoverDate,
            db.AddParameter("@_HandoverDate", o.HandoverDate);
            //@_FinishedDate,
            db.AddParameter("@_FinishedDate", o.FinishedDate);
            //@_ConfirmedDate,
            db.AddParameter("@_ConfirmedDate", o.ConfirmedDate);
            //@_DiscountRate,
            db.AddParameter("@_DiscountRate", o.DiscountRate);
            //@_DiscountAmount,
            db.AddParameter("@_DiscountAmount", o.DiscountAmount);
            //@_OriginalDiscountAmount,
            db.AddParameter("@_OriginalDiscountAmount", o.OriginalDiscountAmount);
            //@_Closed,
            db.AddParameter("@_Closed", o.Closed);
            //@_IsActive,
            db.AddParameter("@_IsActive", o.IsActive);
            //@_CreatedBy,
            db.AddParameter("@_CreatedBy", o.CreatedBy);
            //@_CreatedAt
            db.AddParameter("@_CreatedAt", DateTime.Now.ToShortDateString());
            db.ExecuteNonQuery("sp_B30BizDocDetail_Insert", CommandType.StoredProcedure);
        }
        /// <summary>
        /// Cập nhật sản phẩm
        /// </summary>
        /// <param name="o"></param>
        public static void Update(B30BizDocs o)
        {
            DBHelp db = new DBHelp();
            //@_Id
            db.AddParameter("@_Id", o.Id);
            //@_ParentId,
            db.AddParameter("@_ParentId", o.ParentId);
            //@_IsGroup,
            db.AddParameter("@_IsGroup", o.IsGroup);
            //@_BizDocId,
            db.AddParameter("@_BizDocId", o.BizDocId);
            //@_ParentBizDocId,
            db.AddParameter("@_ParentBizDocId", o.ParentBizDocId);
            //@_DocCode,
            db.AddParameter("@_DocCode", o.DocCode);
            //@_DocDate,
            db.AddParameter("@_DocDate", o.DocDate);
            //@_DocNo,
            db.AddParameter("@_DocNo", o.DocNo);
            //@_Description,
            db.AddParameter("@_Description", o.Description);
            //@_CustomerCode,
            db.AddParameter("@_CustomerCode", o.CustomerCode);
            //@_ContactCode,
            db.AddParameter("@_ContactCode", o.ContactCode);
            //@_ContactPerson,
            db.AddParameter("@_ContactPerson", o.ContactPerson);
            //@_Address,
            db.AddParameter("@_Address", o.Address);
            //@_CurrencyCode,
            db.AddParameter("@_CurrencyCode", o.CurrencyCode);
            //@_ExchangeRate,
            db.AddParameter("@_ExchangeRate", o.ExchangeRate);
            //@_ShipViaCode,
            db.AddParameter("@_ShipViaCode", o.ShipViaCode);
            //@_DocStatus,
            db.AddParameter("@_DocStatus", o.DocStatus);
            //@_DueDate,
            db.AddParameter("@_DueDate", o.DueDate);
            //@_HandoverDate,
            db.AddParameter("@_HandoverDate", o.HandoverDate);
            //@_FinishedDate,
            db.AddParameter("@_FinishedDate", o.FinishedDate);
            //@_ConfirmedDate,
            db.AddParameter("@_ConfirmedDate", o.ConfirmedDate);
            //@_DiscountRate,
            db.AddParameter("@_DiscountRate", o.DiscountRate);
            //@_DiscountAmount,
            db.AddParameter("@_DiscountAmount", o.DiscountAmount);
            //@_OriginalDiscountAmount,
            db.AddParameter("@_OriginalDiscountAmount", o.OriginalDiscountAmount);
            //@_Closed,
            db.AddParameter("@_Closed", o.Closed);
            //@_IsActive,
            db.AddParameter("@_IsActive", o.IsActive);
            //@_ModifiedBy,
            db.AddParameter("@_ModifiedBy", o.ModifiedBy);
            //@_ModifiedAt
            db.AddParameter("@_ModifiedAt", DateTime.Now.ToShortDateString());
            db.ExecuteNonQuery("sp_B30BizDoc_Update", CommandType.StoredProcedure);
        }
        /// <summary>
        /// Xóa sản phẩm theo thể loại
        /// </summary>
        /// <param name="o"></param>
        public static void Delete(B30BizDocs o)
        {
            DBHelp db = new DBHelp();
            db.AddParameter("@_Id", o.Id);
            db.ExecuteNonQuery("sp_B30BizDoc_Delete", CommandType.StoredProcedure);
        }
        #endregion
    }
}